package com.bwhx.eruler.dao.impl;

import java.io.PrintWriter;
import java.io.Serializable;
import java.io.StringWriter;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.springframework.beans.factory.annotation.Autowired;

import com.bwhx.eruler.bean.Page;
import com.bwhx.eruler.dao.BaseDao;

public abstract class BaseDaoImpl<T> extends SqlSessionDaoSupport implements BaseDao<T>{
	
	public Log log = LogFactory.getLog(this.getClass());
	
	public String getTrace(Throwable t){
		
		StringWriter stringWriter= new StringWriter();
		PrintWriter writer= new PrintWriter(stringWriter);
		t.printStackTrace(writer);
		StringBuffer buffer= stringWriter.getBuffer();
		return buffer.toString();
	}
	
	@Autowired
	//mybatis-spring 1.0无需此方法；mybatis-spring1.2必须注入。
	public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory){
		super.setSqlSessionFactory(sqlSessionFactory);
	}
	
	private String ns;		//命名空间
	public String getNs() {
		return ns;
	}
	public void setNs(String ns) {
		this.ns = ns;
	}

	public List<T> find(T entiy) {
		List<T> oList = this.getSqlSession().selectList(ns + "find", entiy);
		return oList;
	}
	public T get(Serializable id) {
		return this.getSqlSession().selectOne(ns + "get", id);
	}

	public void insert(T entity) {
		this.getSqlSession().insert(ns + "insert", entity);
	}

	public void update(T entity) {
		this.getSqlSession().update(ns + "update", entity);
	}

	public void delete(Serializable id) {
		this.getSqlSession().delete(ns + "delete", id);
	}

	public void delete(Serializable[] ids) {
		this.getSqlSession().delete(ns + "deleteBatch", ids);
	}
	 
	
	@Override
	public Integer count(T t) {
		
		return this.getSqlSession().selectOne(ns + "count", t);
	}

	@Override
	public List<T> list(Page p) {
		return this.getSqlSession().selectList(ns + "list", p);
	}
	@Override
	public void delete(T entity) {
		this.getSqlSession().update(ns + "delete", entity);
	}
}
